/*
 * Copyright 2012 Gregor Schauer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.schauer.gregor.vienna.identifier;

import at.schauer.gregor.vienna.exception.IdentifierGenerationException;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ClassUtils;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gregor Schauer
 */
public class MethodInvocationStringGenerator<O> implements IdentifierGenerator<String, O> {
	public static final Object[] NO_ARGS = new Object[0];
	protected String separator = " ";
	protected List<MethodInvocation> invocations;

	public MethodInvocationStringGenerator(MethodInvocation... invocations) {
		addInvocations(invocations);
	}

	public MethodInvocationStringGenerator(String separator, MethodInvocation... invocations) {
		this(invocations);
		this.separator = separator;
	}

	public void addInvocations(MethodInvocation... methodInvocations) {
		CollectionUtils.addAll(getInvocations(), methodInvocations);
	}

	@Override
	public String generate(O obj) {
		try {
			StringBuilder builder = new StringBuilder();
			for (MethodInvocation invocation : invocations) {
				builder.append(separator).append(getValue(obj, invocation));
			}
			return builder.length() > 0 ? builder.substring(1) : builder.toString();
		} catch (Exception e) {
			throw new IdentifierGenerationException(e);
		}
	}

	protected Object getValue(O obj, MethodInvocation invocation) throws InvocationTargetException, IllegalAccessException {
		Method method = invocation.getMethod();
		method.setAccessible(true);
		return method.invoke(obj, invocation.getArguments());
	}

	public List<MethodInvocation> getInvocations() {
		return invocations == null ? invocations = new ArrayList<MethodInvocation>() : invocations;
	}

	public static class SimpleMethodInvocation implements MethodInvocation {
		Method method;
		Object[] args;
		Object target;

		public SimpleMethodInvocation(Class<?> clazz, String methodName, Object... args) {
			this(ReflectionUtils.findMethod(clazz, methodName, ClassUtils.toClass(args)), args);
		}

		public SimpleMethodInvocation(Method method, Object... args) {
			this.method = method;
			this.args = args;
		}

		@Override
		public Method getMethod() {
			return method;
		}

		@Override
		public Object[] getArguments() {
			return args == null ? args = NO_ARGS : args;
		}

		@Override
		public Object proceed() throws Throwable {
			Assert.notNull(target, "Target instance must not ne null.");
			return MethodUtils.invokeMethod(target, method.getName(), args);
		}

		@Override
		public Object getThis() {
			return target;
		}

		public void setThis(Object target) {
			this.target = target;
		}

		@Override
		public AccessibleObject getStaticPart() {
			return method;
		}
	}
}
